//
//  GroupView.m
//  MHO
//
//  Created by papo on 4/12/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "GroupView.h"
#import "Group.h"
#import "Lib.h"

@implementation GroupView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)awakeFromNib {
    [super awakeFromNib];
    
//    self.layer.cornerRadius = 10;
//    self.backgroundColor = [UIColor lightGrayColor];
    
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = 4;
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self addGestureRecognizer:tap];
}

- (void)setGroup:(Group *)group {
    _group = group;
    
    //
    UIImage* placeholderImage;
    if ([self.groupId isEqualToString:@"2"]) {
        placeholderImage = [UIImage imageNamed:@"icon_demo_app.png"];
    }
    else {
        placeholderImage = [UIImage imageNamed:@"icon_demo_game.png"];
    }
    
    [self.iconImage setImageWithURL:[NSURL URLWithString:group.imageUrl] placeholderImage:placeholderImage];
    self.groupName.text = group.name;
//    self.backgroundColor = [Lib colorFromHexString:group.background];
    self.numberGamesLb.text = [NSString stringWithFormat:@"%@ games", group.count];
    self.numberViewsLb.text = [NSString stringWithFormat:@"%@ lượt xem", group.views];
}

- (void)tapGesture:(UIGestureRecognizer*)gesture {
    if (_delegate && [_delegate respondsToSelector:@selector(groupView:clickAtIndex:)]) {
        [_delegate groupView:self clickAtIndex:self.indexPath];
    }
}

+ (GroupView*)viewFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"GroupView" owner:self options:nil] objectAtIndex:0];
}

@end
